﻿using System;
using System.Collections.Generic;

using System.Text;

namespace System.Qizx.Xml
{
    /*******************************/
    /// <summary>
    /// Manages the source XML document that will be transformed.
    /// </summary>
    public class SaxSource : BasicSource
    {
        private string id;
        private XmlSource source;
        private XmlSAXDocumentManager reader;

        /// <summary>
        /// Gets and sets the Id of the SaxSourceSupport instance.
        /// </summary>
        public virtual string Id
        {
            get
            {
                return this.id;
            }
            set
            {
                this.id = value;
                if (this.source == null)
                    this.source = new XmlSource(this.id);
            }
        }

        /// <summary>
        /// Gets and sets the XmlSource of the SaxSourceSupport instance.
        /// </summary>
        public virtual XmlSource Source
        {
            get
            {
                return this.source;
            }
            set
            {
                this.source = value;
            }
        }

        /// <summary>
        /// Gets and sets the XmlSAXDocumentManager of the SaxSourceSupport instance.
        /// </summary>
        public virtual XmlSAXDocumentManager Reader
        {
            get
            {
                return this.reader;
            }
            set
            {
                this.reader = value;
            }
        }

        /// <summary>
        /// Creates a new instance of SaxSourceSupport with the specified XmlSAXDocumentManager and 
        /// XmlSource.
        /// </summary>
        /// <param name="Reader">The XmlSAXDocumentManager used to parse the XML.</param>
        /// <param name="Source">The XmlSource with the XML.</param>
        public SaxSource(XmlSAXDocumentManager Reader, XmlSource Source)
        {
            this.id = (Source.Uri != null) ? Source.Uri : "";
            this.source = Source;
            this.reader = Reader;
        }

        /// <summary>
        /// Creates a new instance of SaxSourceSupport with the specified XmlSource support.
        /// </summary>
        /// <param name="Source">The XmlSource with the XML.</param>
        public SaxSource(XmlSource Source)
        {
            this.id = (Source.Uri != null) ? Source.Uri : "";
            this.source = Source;
            this.reader = new XmlSAXDocumentManager();
        }

        /// <summary>
        /// Creates an empty new instance of SaxSourceSupport.
        /// </summary>	
        public SaxSource()
        {
            this.id = "";
            this.source = null;
            this.reader = null;
        }

        /// <summary>
        /// Creates a new instance of XmlSource from a BasicSourceSupport class.
        /// </summary>
        /// <param name="Source">The BasicSourceSupportClass to be used.</param>
        /// <returns>A new instance of XmlSource class.</returns>
        public static XmlSource FromGenericSource(BasicSource Source)
        {
            XmlSource result = null;
            if (Source is GenericSource)
            {
                GenericSource temp_Source = (GenericSource)Source;
                if (temp_Source.Path != null)
                    result = new XmlSource(temp_Source.Path);
                else
                {
                    if (temp_Source.Reader != null)
                        result = new XmlSource(temp_Source.Reader);
                    else
                        if (temp_Source.Stream != null)
                            result = new XmlSource(temp_Source.Stream);
                }
            }
            else
            {
                if (Source is SaxSource)
                {
                    result = ((SaxSource)Source).Source;
                }

            }
            return result;
        }
    }
}
